<!--
 * @Description: 事件
 * @Author: charles
 * @Date: 2021-11-09 09:22:10
 * @LastEditors: charles
 * @LastEditTime: 2021-11-10 11:29:35
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>表单</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
</head>
<body>
  <div id="app"></div>

  <script type="text/babel">

   
    class App extends React.Component {

      constructor(props){
        super(props)
        this.state = {
          form :{
            name:"默认值",
            age:1
          }
        }
        // 包装防抖函数
        this.handleFormChage = _.debounce(this.handleFormChage,100)
      }
      handleFormChage(event,key){
        let val = event.target.value;
        this.setState({
          form:{
            ...this.state.form,
            [key]:val
          }
        })
      }
      submitHandler(event){
        event.preventDefault();
        alert(JSON.stringify(this.state.form));
      }

      render(){
        return (
          <div>
            {JSON.stringify(this.state.form)}
            <form onSubmit={()=>{
              this.submitHandler(event)
            }}>
              <div>
                姓名
                <input value={this.state.form.name} type="text" onChange={()=>{
                  this.handleFormChage(event,'name')
                }}/>
              </div>  
              <div>
                年龄
                <input value={this.state.form.age}  type="text" onChange={()=>{
                  this.handleFormChage(event,'age')
                }}/>
              </div>  
              <div>
                <input type="submit"/>
              </div>
            </form>
          </div>
        )
      }
    }
    ReactDOM.render(<App/>,document.getElementById('app'))
  </script>
</body>
</html>